package com.example.petback.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.petback.entity.MsgEntity;
import com.example.petback.model.ItemModel;
import com.example.petback.service.ItemService;
import com.example.petback.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("petback/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @RequestMapping("/getItemByCategory/{category}")
    public MsgEntity getItemByCategory(@PathVariable String category) {
        List<ItemModel> list = itemService.getItemByCategory(category);
        return new MsgEntity("SUCCESS", "200", list);
    }

    @RequestMapping("/getItemById/{id}")
    public MsgEntity getItemById(@PathVariable String id) {
        ItemModel item = itemService.getItemById(id);
        return new MsgEntity("SUCCESS", "200", item);
    }

    @PostMapping("/importPicture")
    public MsgEntity importPicture(@RequestParam("file") MultipartFile file, @RequestParam("id") String id) throws FileNotFoundException {
        //获得文件的后缀
        String filename = UUID.randomUUID() + Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
        //获取文件存储路径
        File filepath = new File(Constant.ITEM_PICTURE_PATH + filename);
        String url = String.valueOf(filepath);
        try {
            //存文件
            file.transferTo(filepath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        itemService.setPictureUrl("http://localhost:8080/temp-image/" + filename, id);
        return new MsgEntity("SUCCESS", "200", url);
    }

    @PostMapping("/updateInfoById")
    public MsgEntity updateInfoById(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        BigDecimal listprice = jsonObject.getBigDecimal("listprice");
        BigDecimal unicost = jsonObject.getBigDecimal("unicost");
        Integer inventory = jsonObject.getInteger("inventory");
        String attr = jsonObject.getString("attr");
        boolean b = itemService.updateInfoById(id, listprice, unicost, inventory, attr);
        return new MsgEntity("SUCCESS", "200", b);
    }

    @PostMapping("/insertItem")
    public MsgEntity insertItem(@RequestParam("picture") MultipartFile picture, @RequestParam("id") String id,@RequestParam("listprice") BigDecimal listprice,@RequestParam("unicost") BigDecimal unicost,@RequestParam("inventory") Integer inventory,@RequestParam("attr") String attr,@RequestParam("category") String category){
        String filename = UUID.randomUUID() + Objects.requireNonNull(picture.getOriginalFilename()).substring(picture.getOriginalFilename().lastIndexOf("."));
        File filepath = new File(Constant.ITEM_PICTURE_PATH + filename);
        try {
            picture.transferTo(filepath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ItemModel itemModel = new ItemModel(id, listprice, unicost, 1, inventory, attr, "http://localhost:8080/itempic/" + filename, category);
        boolean b = itemService.insertItem(itemModel);
        return new MsgEntity("SUCCESS","200",b);
    }

    @PostMapping("/updateItem")
    public MsgEntity updateItem(@RequestParam("picture") MultipartFile picture, @RequestParam("id") String id,@RequestParam("listprice") BigDecimal listprice,@RequestParam("unicost") BigDecimal unicost,@RequestParam("inventory") Integer inventory,@RequestParam("attr") String attr){
        String filename = UUID.randomUUID().toString() + Objects.requireNonNull(picture.getOriginalFilename()).substring(picture.getOriginalFilename().lastIndexOf("."));
        File filepath = new File(Constant.ITEM_PICTURE_PATH + filename);
        try {
            picture.transferTo(filepath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        itemService.updateItem(id,listprice,unicost,inventory,attr,"http://localhost:8080/itempic/"+filename);
        return new MsgEntity("SUCCESS","200","b");
    }

    @PostMapping("/changeToNo")
    public MsgEntity changeToNo(@RequestBody JSONObject jsonObject){
        String id = jsonObject.getString("id");
        itemService.changeToNo(id);
        return new MsgEntity("SUCCESS", "200", "OK");
    }

    @PostMapping("/changeToYes")
    public MsgEntity changeToYes(@RequestBody JSONObject jsonObject){
        String id = jsonObject.getString("id");
        itemService.changeToYes(id);
        return new MsgEntity("SUCCESS", "200", "OK");
    }

}
